package com.efast.cafe.portal.controller.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.portal.entity.company.PortalCompanyUserLevel;
import com.efast.cafe.portal.entity.company.PortalCompanyUserLevelUserDto;
import com.efast.cafe.portal.service.company.IPortalCompanyUserLevelService;

/**
 * ****************************************************************************
 * 
 * @File name : PortalCompanyUserLevelController.java
 *
 * @Author : bbqu
 *
 * @Date : 2016-9-5
 *
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-9-5 下午4:55:51 bbqu 1.0
 *            Initial Version
 ****************************************************************************
 */
@Controller
@RequestMapping("/frame_center/company")
public class PortalCompanyUserLevelController extends AbstractController {
	@Autowired
	private IPortalCompanyUserLevelService portalCompanyUserLevelService;

	

	

	@RequestMapping(value = "/userlevel/list")
	public @ResponseBody ResultBean portalCompanyUserLevelList(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute PortalCompanyUserLevel portalCompanyUserLevel, @ModelAttribute QueryPageBase jdt)
			{
		ResultBean result = new ResultBean(true, "");
		Pagination<PortalCompanyUserLevel> pager = super.initPager(jdt.getStart() / jdt.getLength() + 1,
				jdt.getLength(), jdt.getOrderBy(), jdt.getSort());
		result.setObject(
				portalCompanyUserLevelService.queryPagePortalCompanyUserLevelList(pager, portalCompanyUserLevel));
		return result;
	}

	

	@RequestMapping(value = "/userlevel/add")
	public @ResponseBody ResultBean sava(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
			@RequestBody PortalCompanyUserLevel portalCompanyUserLevel) {
		ResultBean result = new ResultBean(true, "");
		try {
			portalCompanyUserLevelService.insertUserlevel(portalCompanyUserLevel);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @Date : 2016-9-7
	 * @param request
	 * @param response
	 * @param id
	 * @param portalCompanyUserLevel
	 * @return
	 */
	@RequestMapping(value = "/userlevel/edit")
	public @ResponseBody ResultBean Edit(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false) Long id, @RequestBody PortalCompanyUserLevel portalCompanyUserLevel) {

		ResultBean result = new ResultBean(true, "");
		try {
			portalCompanyUserLevelService.update(portalCompanyUserLevel);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
		return result;

	}

	/**
	 * 删除
	 * 
	 * @Date : 2016-9-7
	 * @param id
	 * @param request
	 * @param response
	 * @param modelAndView
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/userlevel/delete")
	public @ResponseBody ResultBean delete(@RequestBody Map<String, Integer> map, HttpServletRequest request,
			HttpServletResponse response) {

		ResultBean result = new ResultBean(true, "");
		try {
			if (map.containsKey("portal_company_user_level_id")) {
				Long id = Long.parseLong(map.get("portal_company_user_level_id").toString());
				portalCompanyUserLevelService.del(id);
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
		}
		return result;
	}

	/**
	 * 查询所有用户级别
	 * 
	 * @param request
	 * @param response
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年9月25日
	 */
	@RequestMapping(value = "/userlevel/all")
	public @ResponseBody ResultBean listAll(HttpServletRequest request, HttpServletResponse response) {
		ResultBean result = new ResultBean(true, null);
		try {
			List<PortalCompanyUserLevel> lisCompanyUserLevels = portalCompanyUserLevelService.listUserLevels();
			result.setObject(lisCompanyUserLevels);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 根据级别查询
	 * 
	 * @param request
	 * @param response
	 * @param level
	 * @return
	 */
	@RequestMapping(value = "/userlevel/user")
	public @ResponseBody ResultBean listUser(HttpServletRequest request, HttpServletResponse response, String level) {
		ResultBean result = new ResultBean(true, null);
		try {
			String[] levels = level.split(",");
			List<PortalCompanyUserLevelUserDto> list = new ArrayList<PortalCompanyUserLevelUserDto>();
			List<PortalCompanyUserLevelUserDto> lists = new ArrayList<PortalCompanyUserLevelUserDto>();
			for(int i=0;i<levels.length;i++){
				list = portalCompanyUserLevelService.levelUserList(Long.parseLong(levels[i]));				
				lists.removeAll(list);
				lists.addAll(list);
			}
			result.setObject(lists);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
		}
		return result;
	}
	

}
